<?php
namespace app\system\controller;

use app\common\model\AreaModel;
use app\common\model\AuthRuleModel;
use app\common\model\CmsCategoryModel;
use app\common\model\CmsMoudleModel;
use app\common\model\ConfigModel;
use app\common\model\DictModel;
use app\common\model\ShopExpressModel;
use app\common\model\ShopGoodsBrandModel;
use app\common\model\ShopGoodsCategoryModel;
use app\common\model\ShopOrderModel;
use app\common\model\UserModel;
use think\Db;

class IndexController extends BaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $userModel = new UserModel();
        $userinfo  = $userModel->getInfo($this->getUtype(), $this->getUid());
        $title     = $userinfo['site_id'] > 0 ? get_field_value('user_site', 'site_name', $userinfo['site_id']) : '控制台';
        $ruleList  = $userModel->getMyRuleList($userinfo['group_id'], $userinfo['utype']);
        $this->assign('title', $title);
        $this->assign('userinfo', $userinfo);
        $this->assign('siteId', $this->getSiteId());
        $this->assign('ruleList', $ruleList);
        return $this->fetch();
    }

    /**
     * 获取待办事项
     */
    public function getTodo(){
        $map['site_id'] = $this->getSiteId();
        $static['t1'] = Db::table('shop_order')->where($map)->where('pay_status=0 and order_status>=0')->count(); //未付款订单
        $static['t2'] = Db::table('shop_order')->where($map)->where('pay_status=1 and order_status=0')->count(); //待确认订单
        $static['t3'] = Db::table('shop_order')->where($map)->where('pay_status=1 and shipping_status=0')->count(); //代发货订单
        $static['t4'] = Db::table('shop_order_refund')->where($map)->where('refund_status=1')->count(); //退款申请单
        $static['t5'] = Db::table('shop_goods_geval')->where($map)->where('geval_status=0')->count(); //待处理订单评价
        $static['t6'] = Db::table('shop_order')->where($map)->where('order_status=2 and invoice=1')->count(); //待开发票
        $static['t7'] = Db::table('shop_promotion_auction_log')->where('status=1 and winning=1')->count(); //待处理竞拍
        $static['t8'] = Db::table('shop_promotion_groupbuy_cart')->where($map)->where('is_order=0')->count(); //待处理集采
        $sum = 0;
        foreach ($static as $v){
            $num = empty($v)?0:$v;
            $sum += $num;
        }
        $static['total'] = $sum;
        return $static;
    }


    /**
     * 主窗口
     */
    public function main()
    {
        return $this->fetch();
    }

    /**
     * 初始化数据
     */
    public function initCache()
    {
        if ($this->getUtype() == 'system') {
            $areaModel        = new AreaModel();
            $cmsMoudleModel   = new CmsMoudleModel();
            $dictModel        = new DictModel();
            $shopExpressModel = new ShopExpressModel();
            $areaModel->updateCache(); //更新地区缓存
            $arr[] = "地区缓存[area]更新完毕！";
            $cmsMoudleModel->updateCache(); //更新cms模型缓存
            $arr[] = "模型缓存[cms_moudle]、字段缓存[cms_field[1,2]]、推荐位缓存[cms_flag[1,2]]更新完毕！";
            $dictModel->updateCache(); //更新字典缓存
            $arr[] = "字典缓存[dict]更新完毕！";
            $shopExpressModel->updateCache(); //更新快递公司缓存
            $arr[] = "快递公司缓存[express]更新完毕！";
        }
        $cmsCategoryModel   = new CmsCategoryModel();
        $configModel        = new ConfigModel();
        $goodsBrandModel    = new ShopGoodsBrandModel();
        $goodsCategoryModel = new ShopGoodsCategoryModel();
        $authRuleModel      = new AuthRuleModel();
        $authRuleModel->updateCache($this->getUtype()); //更新权限规则缓存
        $arr[] = "规则缓存[auth_rule_{$this->getUtype()}]更新完毕！";
        $cmsCategoryModel->updateCache($this->getSiteId()); //更新cms栏目缓存
        $arr[] = "cms栏目缓存[cms_category{$this->getSiteId()}]更新完毕！";
        $configModel->updateCache($this->getSiteId()); //更新配置缓存
        $arr[] = "站点配置缓存[webconfig{$this->getSiteId()}]更新完毕！";
        $goodsBrandModel->cidBrandCache($this->getSiteId()); //更新品牌栏目缓存
        $arr[] = "品牌栏目缓存[brand_cid{$this->getSiteId()}]更新完毕！";
        $goodsCategoryModel->updateCache($this->getSiteId()); //更新商品类目缓存
        $arr[] = "商品类目缓存[goods_category{$this->getSiteId()}]更新完毕！";
        $this->assign('arr', $arr);
        return $this->fetch();
    }

    /**
     * 退出登录
     */
    public function exitLogin()
    {
        switch ($this->getUtype()) {
            case 'system':
                $url = url('Public/sysLogin');
                break;
            case 'site':
                $url = url('Public/siteLogin');
                break;
            case 'operator':
                $url = url('Public/operatorLogin');
                break;
            default:
                $url = url('Public/sysLogin');
                break;
        }
        cookie($this->getUtype() . 'Des', null);
        $this->success('退出登录成功！', $url);
    }

}
